package ecustom.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import weaver.interfaces.schedule.BaseCronJob;

/**
 * 计划任务抽象类。
 * @author 曹水涛
 */
public abstract class AbstractCronJob extends BaseCronJob {

	private static List<String> currTasks = new ArrayList<String>();	// true 表示正在执行上一轮任务

	private Logger log = Logger.getLogger(this.getClass());

	@Override
	public void execute() {
		if (currTasks.contains(this.getClass().getName())) {
			log.error("last task is not over!");
			return ;
		}

		long time = System.currentTimeMillis();
		try {
			log.info("task start...");
			currTasks.add(this.getClass().getName());
			start();
		} catch (Exception e) {
			log.error("task error", e);
		} finally {
			currTasks.remove(this.getClass().getName());
			time = System.currentTimeMillis() - time;
			log.info("total time spent(s): " + (time / 1000));
			log.info("task is over!");
		}
	
	}

	protected Logger log() {
		return log;
	}

	protected abstract void start() throws Exception;

}
